﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Expenses.DAL.Interfaces;
using Expenses.Domain.Entities;
using NHibernate;

namespace Expenses.DAL.Repositories
{
    public class InvoiceRepository : IInvoiceRepository
    {
        public void Add(Invoice invoice)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(invoice);
                transaction.Commit();
            }
        }

        public void Update(Invoice invoice)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(invoice);
                transaction.Commit();
            };
        }

        public void Remove(Invoice invoice)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(invoice);
                transaction.Commit();
            }
        }

        public Invoice GetById(Guid invoiceId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Invoice>(invoiceId);
        }

        public Invoice GetByName(string name)
        {
            throw new NotImplementedException();
        }

        public ICollection<Invoice> GetAll()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ICollection<Invoice> invoices = session
                    .CreateCriteria(typeof(Invoice))
                    //.CreateAlias("Status", "s")
                    .List<Invoice>();

                return invoices;
            }

        }
    }
}
